using System;
using System.Threading;
using FreeSwitch.EventSocket.Commands;

namespace FreeSwitch.EventSocket
{
	public class AsyncCommand : IAsyncResult
	{
		public AsyncCommand()
		{
			Event = new ManualResetEvent(false);
		}
		internal AsyncCallback Callback { get; set; }
		internal ICommand Command { get; set; }
		internal ManualResetEvent Event { get; set; }
		internal ICommandReply Reply { get; set; }

		#region IAsyncResult Members

		/// <summary>
		/// Gets a value that indicates whether the asynchronous operation has completed.
		/// </summary>
		/// <returns>
		/// true if the operation is complete; otherwise, false.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		public bool IsCompleted { get; internal set; }

		/// <summary>
		/// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		public WaitHandle AsyncWaitHandle
		{
			get { return Event; }
		}

		/// <summary>
		/// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
		/// </summary>
		/// <returns>
		/// A user-defined object that qualifies or contains information about an asynchronous operation.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		public object AsyncState { get; internal set; }

		/// <summary>
		/// Gets a value that indicates whether the asynchronous operation completed synchronously.
		/// </summary>
		/// <returns>
		/// true if the asynchronous operation completed synchronously; otherwise, false.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		public bool CompletedSynchronously
		{
			get { return false; }
		}

		#endregion

		public override string ToString()
		{
			return "Async: " + Command;
		}
	}
}